//
//  IFMoviePlayerController.m
//  IfengNews
//
//  Created by 刘潋 on 14-10-24.
//
//

#import "IFMoviePlayerController.h"
#import "UIView+Utils.h"
#import "IFAudioPlayerManager.h"
@interface IFMoviePlayerController ()
@property (nonatomic, strong) NSTimer *statisticalTimer;
@property (nonatomic, unsafe_unretained) NSInteger videohash;
@property (nonatomic,strong) UIActivityIndicatorView *activityView;

@end

@implementation IFMoviePlayerController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMovieDurationAvailableNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
}

- (id)initWithContentURL:(NSURL *)url WithType:(IFVideo_Type)type
{
    if (self = [super initWithContentURL:url]) {
        self.controlStyle = MPMovieControlStyleEmbedded;
        self.scalingMode = MPMovieScalingModeAspectFit;
        _videoType = type;
        _playingtime = 0.;
        _videohash = [self hash];
        self.activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        self.activityView.frame=CGRectMake(0, 0, 45.f, 45.f);
        [self _addMovieObservers];
    }
    return self;
}

- (void)prepareToPlay{
    [self.activityView setCenter:CGPointMake(self.view.width/2.f, self.view.height/2.f)];
    [self.view addSubview:self.activityView];
    [self.activityView startAnimating];

    [super prepareToPlay];
}
- (void)play{
    if([IFAudioPlayerManager shareInstance].isPlaying){
        [[IFAudioPlayerManager shareInstance] stop];
    }
    
       [super play];
    
}

- (void)_addMovieObservers
{
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDurationAvailable:) name:MPMovieDurationAvailableNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
}

- (void)moviePlaybackStateDidChange:(NSNotification *)note {
    
    if(note.object!=self)return;
    switch (self.playbackState) {
        case MPMoviePlaybackStatePlaying:
        {
            
            
            if ([self.delegate respondsToSelector:@selector(IFMoviePlaying)]) {
                [self.delegate IFMoviePlaying];
            }
            
            self.statisticalTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(statistical) userInfo:nil repeats:YES];
            [self.activityView stopAnimating];
            [self.activityView removeFromSuperview];
            
            break;
        }
        case MPMoviePlaybackStateSeekingBackward:
        {
            [self.activityView setCenter:CGPointMake(self.view.width/2.f, self.view.height/2.f)];
            [self.view addSubview:self.activityView];
            [self.activityView startAnimating];

            break;
        }
        case MPMoviePlaybackStateSeekingForward:
        {
            [self.activityView setCenter:CGPointMake(self.view.width/2.f, self.view.height/2.f)];
            [self.view addSubview:self.activityView];
            [self.activityView startAnimating];

            break;
        }
        case MPMoviePlaybackStateInterrupted:
        {
            NSLog(@"444");
            break;
        }
        case MPMoviePlaybackStatePaused:
        {
            [self.statisticalTimer invalidate];
            self.statisticalTimer = nil;
            break;
        }
        case MPMoviePlaybackStateStopped:
        {
            [self.statisticalTimer invalidate];
            self.statisticalTimer = nil;
            break;
        }
        default:
            break;
    }
}

- (void)movieFinished:(NSNotification *)note {
    
    if (_videohash != [self hash]) {
        return;
    }
    
    [self.statisticalTimer invalidate];
    self.statisticalTimer = nil;
   
    if ([self.delegate respondsToSelector:@selector(IFMovieFishedWithType:)]&&note.object==self) {
        [self.delegate IFMovieFishedWithType:_videoType];
    }
}

- (void)movieDurationAvailable:(NSNotification *)note {
    
}

- (void)movieLoadStateDidChange:(NSNotification *)note {
    if(note.object!=self)return;
    switch (self.loadState) {
        case MPMovieLoadStatePlayable:{
            [self.activityView stopAnimating];
            [self.activityView removeFromSuperview];
            break;
        }
        case MPMovieLoadStatePlaythroughOK:{
        
            break;
        }
        case MPMovieLoadStateStalled:{
         
            break;
        }
        case MPMovieLoadStateUnknown:{
          
            break;
        }
        default:
            break;
    }
}

- (void)statistical
{
    _playingtime++;
}

@end
